<!--
* description: 采购销退申请
* author: wangjuan
* createdDate: 2021-3-1
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="false"
      :dynamic-is-init-table="true"
    />

    <edit
      :key="editPopupsKey"
      :dynamic-edit-row-data="editRowData"
      :popups-visible.sync="editPopupsVisible"
      :popups-state="editPopupsState"
    />
  </div>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import api from '@/api/pa/procuremenData'

export default {
  name: 'PurchasePinBackApplication',

  components: {
    OneTableTemplate,
    Edit: () => import('./edit')
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      editRowData: {},
      editPopupsVisible: false,

      // 网格查询API配置对象
      apiConfig: api.purchasePinBackQueryByPage,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'add',
          clickEvent: this.add,
          text: this.$t('sys.button.add'), // 新增
          position: 'left'
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '销退类型：',
              codeField: 'purReturnType',
              textField: 'purReturnTypeName',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'purReturnType,purReturnTypeName',
              options: [
                { purReturnType: '440', purReturnTypeName: '采购销退' },
                { purReturnType: '550', purReturnTypeName: '呆滞件销退' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '销退单号：',
              codeField: 'purReturnCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '备件编码：',
              codeField: 'partNo',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '备件名称：',
              codeField: 'partName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '销退日期：',
              codeField: 'returnDate',
              component: () => import('@/components/org/datePicker/andtDatePicker'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '关联单号：',
              codeField: 'associateOrderId',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '状态：',
              codeField: 'returnStatus',
              textField: 'returnStatusName',
              oFields: 'returnStatus,returnStatusName',
              customObj: {
                api: api.queryDropDown,
                listQuery: {
                  returnStatus: ''
                },
                selectType: '1'
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'purReturnId',
              label: '主键ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'controlBtn1',
              label: '操作',
              width: 150,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '修改',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.edit(index)
                },
                {
                  compKey: 'propKey2',
                  labelName: '提交',
                  // rowFileds: 'show:propKey1-已提交',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.submit(index)
                },
                {
                  compKey: 'propKey3',
                  labelName: '作废',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: (index) => this.invalid(index)
                },
                {
                  compKey: 'propKey4',
                  labelName: '附件',
                  // rowFileds: 'show:propKey1-否',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: () => this.attachment('parts')
                }
              ]
            },
            {
              prop: 'purReturnCode',
              label: '销退单号',
              width: 140
            },
            {
              prop: 'purReturnTypeName',
              label: '销退类型'
            },
            {
              prop: 'returnStatus',
              label: '状态'
            },
            {
              prop: 'returnDate',
              label: '销退日期',
              width: 140
            },
            {
              prop: 'returnAmount',
              label: '销退总金额'
            },
            {
              prop: 'returnReason',
              label: '销退申请原因',
              width: 140
            },
            {
              prop: 'associateOrderId',
              label: '关联单号',
              width: 140
            },
            {
              prop: 'annex',
              label: '是否上传附件'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(this, {
        purReturnId: '',
        updateControlId: '',
        purReturnType: '',
        purReturnCode: '',
        dlrCode: '',
        dlrShortName: '',
        returnDate: '',
        associateOrderId: '',
        partNo: '',
        partName: '',
        returnStatus: '',
        lookupValueName: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 编辑获取数据
    edit(index) {
      this.editPopupsState = 'edit'
      this.editPopupsVisible = true
      this.editRowData = this.$utils.deepCopy(this.$refs['multipleTable'].list[index])
    },
    add() {
      this.editPopupsState = 'add'
      this.editPopupsVisible = true
    },

    // 提交
    submit(index) {
      // debugger
      this.$confirm('确定要提交吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.purchasePinBackSubmit,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: this.$refs.multipleTable.list[index]
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      }).catch(err => {
        console.log(err)
      })
    },

    // 作废
    invalid(index) {
      // debugger
      this.$confirm('确定要作废吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.purchasePinBackInvalid,
          // 条件/实体参数（变量），根据typeParam中的定义配置
          variables: this.$refs.multipleTable.list[index]
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
